#!/usr/bin/perl

push @INC, "$ARGV[0]/Contents/Resources";
require "script_vars.pl";
require "SharedScript.pl";

# plug-in folder names:
#	"Modules externes",		# French
#	"Zusatzmodule",			# German
#	"Pasta de plug-ins",	# Portugese
#	"Plugins",				# Spanish
#	"Insteekmodules",		# Dutch
#	"Tillgg",				# Swedish
#	"Plug-in",				# Italian
#	"Plug-Ins",				# English
# we put the English after the other names so that if for some reason
# a folder called Plug-ins exists we will ignore it for localized name
my @PLUG_IN_DIRS = (
	"Modules externes",
	"Zusatzmodule",
	"Pasta de plug-ins",
	"Plugins",
	"Insteekmodules",
	"Tillgg",
	"Plug-in",
	"Plug-Ins",
	"Support Files/Plug-Ins",		#elements 10
	"Contents/Plug-Ins",			#appstore
);

my @SUPPORTED_APPS = @PHOTOSHOPS_SUPPORTED;
my @PLUGIN_DIRS;
my @PRODUCT_FOLDERS_TO_DELETE;

# can use this line for debugging
#my $RES = "/Volumes/LaCieDisk/projects/InstallerSources/Installers/Intellihance/Mac/Resources/";
my $RES = "$ARGV[0]/Contents/Resources";
my $INST_TOOL = "$RES/installer_tool.app/Contents/MacOS/installer_tool";
my $MERGE_TOOL = "$RES/MergeTool";

exit main();


###############################################################################
#
# CleanPerms()
#
# This function ensures the ownership/permissions of the installed files
# are correct.
#
###############################################################################
sub CleanPerms
{
   my $cmd = "";
   my $me = MyUID();
   
   print "POSTFLIGHT::CleanPerms> me = $me\n";
   
   # Make sure the Applications folder is still root:admin
   if (-d "/Applications")
   {
      $cmd = "chown root:admin /Applications";
      print "POSTFLIGHT::CleanPerms> $cmd\n";
      `$cmd`;

      $cmd = "chmod 775 /Applications";
      print "POSTFLIGHT::CleanPerms> $cmd\n";
      `$cmd`;
   }

   # Ensure that our product folder in the Applications folder is set to <user_name>:admin
   if (-d "/Applications/$FULL_PRODUCT_NAME")
   {
      $cmd = "chown -R $me:admin \"/Applications/$FULL_PRODUCT_NAME\"";
      print "POSTFLIGHT::CleanPerms> $cmd\n";
      `$cmd`;

      $cmd = "chmod -R 777 \"/Applications/$FULL_PRODUCT_NAME\"";
      print "POSTFLIGHT::CleanPerms> $cmd\n";
      `$cmd`;
   }
}



###############################################################################
#
# FindUniquePSPluginDirs()
#
# Find all the unique Photoshop plug-in directories installed on this
# machine. Adobe places a file in the Preferences directory of each
# user who installs (or runs) Photoshop. We look for all these
# files, read them and compile a list of unique plug-in directories.
# This list is returned when the function completes.
#
###############################################################################
sub FindUniquePSPluginDirs
{
	print "POSTFLIGHT::FindUniquePSPluginDirs>\n";

	my $user = "";
	my $path = "";
	my $ps_path = "";
	my $pi_path = "";
	my @pluginDirs;

	# The following list of users may contain
	# bogus users, such as "Shared", or "Deleted Users".
	# This is OK, since the Adobe file we are looking for
	#in such cases will not be found.
	@users = `ls /Users`;
	foreach $user (@users)
	{
		chomp($user);

		# make sure it is a directory
		if (-d "/Users/$user")
		{
			print "POSTFLIGHT::FindUniquePSPluginDirs> Checking user: $user\n";

			foreach $ps (@SUPPORTED_APPS)
			{
				$path = "/Users/$user/Library/Preferences/$ps Paths";
				print "POSTFLIGHT::FindUniquePSPluginDirs> Looking for path file: $path\n";
				if (-f "$path")
				{
					print "POSTFLIGHT::FindUniquePSPluginDirs> Found path file $path\n";

					# see if we can get the plug-in folder from the path file
					$pi_path = `"$INST_TOOL" readPhotoshopPluginPath "$path"`;
					chop($pi_path);

					if (-d $pi_path)
					{
						print "POSTFLIGHT::FindUniquePSPluginDirs> Found $pi_path in the file $path\n";

						my $ps_only_pi_path = $pi_path . "/Adobe Photoshop Only";

						# if there is an "Adobe Photoshop Only" folder we will install there
						if (-d $ps_only_pi_path)
						{
							if (join(" ", @pluginDirs) !~ "$ps_only_pi_path")
							{
								print "POSTFLIGHT::FindUniquePSPluginDirs> Found $ps_only_pi_path, will install there\n";
								push(@pluginDirs, "$ps_only_pi_path");
							}
						}

						# otherwise, just use the plug-ins folder itself
						else
						{
							if (join(" ", @pluginDirs) !~ "$pi_path")
							{
								print "POSTFLIGHT::FindUniquePSPluginDirs> Found $pi_path, will install there\n";
								push(@pluginDirs, "$pi_path");
							}
						}
					}
					else
					{
						# look for the plug-in folder explicitly
						print "POSTFLIGHT::FindUniquePSPluginDirs> looking for plug-in folder explicitly\n";
						$ps_path = `"$INST_TOOL" readPhotoshopPath "$path"`;
						chop($ps_path);
		
						if (-d $ps_path)
						{
							print "POSTFLIGHT::FindUniquePSPluginDirs> Found path: $ps_path\n";
						}
						else
						{
							print "POSTFLIGHT::FindUniquePSPluginDirs> Could not find: $ps_path\n";
							print "POSTFLIGHT::FindUniquePSPluginDirs> Using: /Applications/$ps\n";
							$ps_path = "/Applications/$ps";
						}

						foreach $plug_in_dir (@PLUG_IN_DIRS)
						{
							$pi_path = $ps_path . "/$plug_in_dir";
		
							if (-d $pi_path)
							{
								my $ps_only_pi_path = $pi_path . "/Adobe Photoshop Only";
		
								# if there is an "Adobe Photoshop Only" folder we will install there
								if (-d $ps_only_pi_path)
								{
									if (join(" ", @pluginDirs) !~ "$ps_only_pi_path")
									{
										print "POSTFLIGHT::FindUniquePSPluginDirs> Found $ps_only_pi_path, will install there\n";
										push(@pluginDirs, "$ps_only_pi_path");
									}
								}
		
								# otherwise, just use the plug-ins folder itself
								else
								{
									if (join(" ", @pluginDirs) !~ "$pi_path")
									{
										print "POSTFLIGHT::FindUniquePSPluginDirs> Found $pi_path, will install there\n";
										push(@pluginDirs, "$pi_path");
									}
								}
							}
						}
					}
				}
				else
				{
					$ps_path = "/Applications/$ps";
					print "POSTFLIGHT::FindUniquePSPluginDirs> Path file not found, looking for: $ps_path\n";

					if (-d $ps_path)
					{
						foreach $plug_in_dir (@PLUG_IN_DIRS)
						{
							$pi_path = $ps_path . "/$plug_in_dir";

							print "POSTFLIGHT::FindUniquePSPluginDirs> Looking for: $pi_path\n";

							if (-d $pi_path)
							{
								my $ps_only_pi_path = $pi_path . "/Adobe Photoshop Only";

								# if there is an "Adobe Photoshop Only" folder we will install there
								if (-d $ps_only_pi_path)
								{
									if (join(" ", @pluginDirs) !~ "$ps_only_pi_path")
									{
										print "POSTFLIGHT::FindUniquePSPluginDirs> Found $ps_only_pi_path, will install there\n";
										push(@pluginDirs, "$ps_only_pi_path");
									}
								}

								# otherwise, just use the plug-ins folder itself
								else
								{
									if (join(" ", @pluginDirs) !~ "$pi_path")
									{
										print "POSTFLIGHT::FindUniquePSPluginDirs> Found $pi_path, will install there\n";
										push(@pluginDirs, "$pi_path");
									}
								}
							}
							else
							{
								print "POSTFLIGHT::FindUniquePSPluginDirs> $pi_path Not found\n";
							}
						}
					}
					else
					{
						print "POSTFLIGHT::FindUniquePSPluginDirs> $ps_path Not found\n";
					}
				}
			}
		}
	}

	my $count = scalar(@pluginDirs);
	print "POSTFLIGHT::FindUniquePSPluginDirs> Found $count places to install\n";
	return @pluginDirs;
}


###############################################################################
# newer
#	returns 1 if the first file is newer that the second, 0 otherwise
###############################################################################
sub newer
{
	use File::stat;
	my $sb1 = stat($_[0]);
	my $sb2 = stat($_[1]);

	# see if the first file is newer than the second
	my $time1 = $sb1->mtime;
	my $time2 = $sb2->mtime;
	if ($time1 > $time2) {
		return 1;
	}
	else {
		return 0;
	}
}


###############################################################################
# copyPrefs
###############################################################################
sub copyPrefs
{
	my $me = MyUID();
	my $user_name = MyUsername();
	my $home_dir = MyHomeDirectory();
	my $pref_dir = "$home_dir/Library/$PREFS_DEST";


	if( -d "$PREFS_SOURCE/$_[0]" )
	{
		# first see if the old prefs folder exists
		# if it does and the new one doesn't make a copy of it
		if(length($OLD_PREFS_FOLDER) > 0)
		{
			my $old_pref_dir = "$home_dir/Library/$OLD_PREFS_FOLDER";
			if ( !-d "$pref_dir/$_[0]" && -d "$old_pref_dir/$_[0]" )
			{
				$cmd = "ditto \"$old_pref_dir/$_[0]\" \"$pref_dir/$_[0]\"";
				print "POSTFLIGHT::main> $cmd\n";
				`$cmd`;
			}
		}
		
		if ($SHOULD_COPY_PREFS_INSTEAD_OF_MOVE == 1)
		{
			$cmd = "ditto -rsrc \"$PREFS_SOURCE/$_[0]\" \"$pref_dir/$_[0]\"";
		}
		else
		{
			unless ( -d "$prefs_dir" )
			{
				$cmd = "mkdir -p \"$pref_dir\"";
				print "POSTFLIGHT::copyPrefs> $cmd\n";
				print `$cmd`;
			}
			$cmd = "mv \"$PREFS_SOURCE/$_[0]\" \"$pref_dir\"";
		}
		print "POSTFLIGHT::copyPrefs> $cmd\n";
		print `$cmd`;
		$cmd = "chown -R $me:admin \"$pref_dir/$_[0]\"";
		print "POSTFLIGHT::copyPrefs> $cmd\n";
		print `$cmd`;
		$cmd = "chmod -R 777 \"$pref_dir/$_[0]\"";
		print "POSTFLIGHT::copyPrefs> $cmd\n";
		print `$cmd`;
	
	   # Ensure that the entire hierarchy that we just installed has the correct permissions
		my $pref_root = "$home_dir/Library/$PREFS_ROOT";
		if (-d "$pref_root")
		{
			$cmd = "mkdir -p \"$pref_root\"";
			print "POSTFLIGHT::copyPrefs> $cmd\n";
			print `$cmd`;
			$cmd = "chown -R $me:admin \"$pref_root\"";
			print "POSTFLIGHT::copyPrefs> $cmd\n";
			print `$cmd`;
			$cmd = "chmod -R 777 \"$pref_root\"";
			print "POSTFLIGHT::copyPrefs> $cmd\n";
			print `$cmd`;
		}
	}
}

###############################################################################
# doCopy
###############################################################################
sub doCopy
{
	print "POSTFLIGHT::doCopy>\n";

	my $me = MyUID();

	my $plug_in_dest;
	my $onone_lib_dest;
	my $onone_palette_dest;
	my $install_x32;			


	foreach $plug_in_dest (@PLUGIN_DIRS)
	{
		#######################################################################
		# Copy the main plug-in files and set the proper owner and permissions
		#######################################################################
		print "POSTFLIGHT::doCopy> Copying $PLUG_IN_SOURCE/$FULL_PRODUCT_NAME to $plug_in_dest/$FULL_PLUGIN_NAME\n";

		# delete any old ones first
		foreach $product_folder (@PRODUCT_FOLDERS_TO_DELETE)
		{
			if (-d "$plug_in_dest/$product_folder" || -f "$plug_in_dest/$product_folder")
			{
				print "POSTFLIGHT::doCopy> Removing old $plug_in_dest/$product_folder\n";
				`rm -rf \"$plug_in_dest/$product_folder\"`;
			}
		}
		
		
		$install_x32 = 0;
		foreach $photoshop_name_comp (@x32_PHOTOSHOP_VERSIONS)
		{
			$photoshop_x32_match = grep(/$photoshop_name_comp/ ,$plug_in_dest);
			
			if ($photoshop_x32_match > 0)
			{
				print "POSTFLIGHT::doCopy> Found 32 bit Photoshop $plug_in_dest\n";
				$install_x32 = 1;
			}
		}
		
		#install the common plugin package
		$cmd = "ditto -rsrc \"$PLUG_IN_SOURCE/$FULL_PLUGIN_NAME\" \"$plug_in_dest/$FULL_PLUGIN_NAME\"";
		print "POSTFLIGHT::doCopy> $cmd\n";
		`$cmd`;
		
		$cmd = "chown -R $me:admin \"$plug_in_dest/$FULL_PLUGIN_NAME\"";
		print "POSTFLIGHT::doCopy> $cmd\n";
		`$cmd`;

		$cmd = "chmod -R 777 \"$plug_in_dest/$FULL_PLUGIN_NAME\"";
		print "POSTFLIGHT::doCopy> $cmd\n";
		`$cmd`;

		if ($install_x32 == 1 && $HAS_SIXTY_FOUR_BIT_HARNESS == 1)
		{
			#install non-harness sub-plugin for x32 photoshop (bug #4360)
			
			#delete harness sub-plugin
			$cmd = "rm -rf \"$plug_in_dest/$FULL_PLUGIN_NAME/$FULL_PLUGIN_NAME\"";
			print "POSTFLIGHT::doCopy> $cmd\n";
			`$cmd`;
			
			#copy non-harness sub-plugin
			$cmd = "ditto -rsrc \"$PLUG_IN_SOURCE/$FULL_PLUGIN_x32\" \"$plug_in_dest/$FULL_PLUGIN_NAME/$FULL_PLUGIN_NAME\"";
			print "POSTFLIGHT::doCopy> $cmd\n";
			`$cmd`;
		}
		
		
		#######################################################################
		# Copy the onOne library and set the proper owner and permissions
		#######################################################################
		if ($SHOULD_INSTALL_ONONE_LIBRARY == 1)
		{
			if ($plug_in_dest =~ /Adobe Photoshop Only/)
			{
				# if we are installing into the "Adobe Photoshop Only" folder we need to go up on level for the onOne Library
				$onone_lib_dest = "$plug_in_dest/..";
			}
			else
			{
				$onone_lib_dest = "$plug_in_dest";
			}

			# if there is a panels directory then assume that we should not install the onOne Library
			# and delete any that is already there
			if (-d "$plug_in_dest/Panels")
			{
				if (-f "$onone_lib_dest/onOne Library.8li" || -d "$onone_lib_dest/onOne Library.8li")
				{
					print "POSTFLIGHT::doCopy> Removing old $onone_lib_dest/onOne Library.8li\n";
					`rm -rf \"$onone_lib_dest/onOne Library.8li\"`;
				}
			}
			else
			{
				print "POSTFLIGHT::doCopy> Copying onOne library to $onone_lib_dest\n";

				my $should_copy_lib = 1;
				if (-f "$onone_lib_dest/onOne Library.8li" || -d "$onone_lib_dest/onOne Library.8li")
				{
					if (newer("$PLUG_IN_SOURCE/onOne Library.8li", "$onone_lib_dest/onOne Library.8li") == 1)
					{
						# delete any old one first
						print "POSTFLIGHT::doCopy> Removing old $onone_lib_dest/onOne Library.8li\n";
						`rm -rf \"$onone_lib_dest/onOne Library.8li\"`;
					}
					else
					{
						print "POSTFLIGHT::doCopy> onOne library is already up to date\n";
						$should_copy_lib = 0;
					}
				}
				if ($should_copy_lib == 1)
				{
					$cmd = "ditto -rsrc \"$PLUG_IN_SOURCE/onOne Library.8li\" \"$onone_lib_dest/onOne Library.8li\"";
					print "POSTFLIGHT::doCopy> $cmd\n";
					`$cmd`;
					$cmd = "chown -R $me:admin \"$onone_lib_dest/onOne Library.8li\"";
					print "POSTFLIGHT::doCopy> $cmd\n";
					`$cmd`;
					$cmd = "chmod -R 777 \"$onone_lib_dest/onOne Library.8li\"";
					print "POSTFLIGHT::doCopy> $cmd\n";
					`$cmd`;
				}
			}
		}

		
		#######################################################################
		# Copy the onOne palette and set the proper owner and permissions
		#######################################################################
		$onone_palette_dest = "$plug_in_dest/Panels";
		if (-d "$onone_palette_dest" && -d "$PLUG_IN_SOURCE")
		{
			print "POSTFLIGHT::doCopy> Copying onOne palette to $onone_palette_dest\n";
	
			my $should_copy_palette = 1;
			if (-d "$onone_palette_dest/onOne Palette")
			{
				if (newer("$PLUG_IN_SOURCE/onOne Palette", "$onone_palette_dest/onOne Palette") == 1)
				{
					# delete any old one first
					print "POSTFLIGHT::doCopy> Removing old $onone_palette_dest/onOne Palette\n";
					`rm -rf \"$onone_palette_dest/onOne Palette\"`;
				}
				else
				{
					print "POSTFLIGHT::doCopy> onOne Palette is already up to date\n";
					$should_copy_palette = 0;
				}
			}
			if ($should_copy_palette == 1)
			{
				$cmd = "ditto -rsrc \"$PLUG_IN_SOURCE/onOne Palette\" \"$onone_palette_dest/onOne Palette\"";
				print "POSTFLIGHT::doCopy> $cmd\n";
				print `$cmd`;
				$cmd = "chown -R $me:admin \"$onone_palette_dest/onOne Palette\"";
				print "POSTFLIGHT::doCopy> $cmd\n";
				print `$cmd`;
				$cmd = "chmod -R 777 \"$onone_palette_dest/onOne Palette\"";
				print "POSTFLIGHT::doCopy> $cmd\n";
				print `$cmd`;
			}
		}
	}

	print "POSTFLIGHT::doCopy> success.\n";
}

###############################################################################
# doPluginInstall
#
# need to explictly install each plugin to support suite installer and updater
###############################################################################
sub doPluginInstall
{
	print "POSTFLIGHT::doPluginInstall>\n";
	
	my $FULL_PRODUCT_NAME_ORIG = $FULL_PRODUCT_NAME;		#$FULL_PRODUCT_NAME changed by each plugin install (below)
	my $FULL_PLUGIN_NAME_ORIG = $FULL_PLUGIN_NAME;
	my $PLUG_IN_SOURCE_ORIG = $PLUG_IN_SOURCE;
	
	my $me = MyUID();

	# get Photoshop installs
	@SUPPORTED_APPS = @PHOTOSHOPS_SUPPORTED;
	my @PSpluginDirs = FindUniquePSPluginDirs();

	# get Elements installs
	@SUPPORTED_APPS = @ELEMENTS_SUPPORTED;
	my @elementsPluginDirs = FindUniquePSPluginDirs();

	#######################################################################
	# See if the Installer is running for this user, if so go ahead and
	# do some interactive things on the system
	# This is to prevent us, for example, from opening a Finder window
	# with root level priveleges on a logged out system
	#######################################################################
	my $my_name = MyUsername();
	$cmd = "ps -U $my_name -w | awk '/Installer.app/ && !/awk/'";
	print "POSTFLIGHT::main> Checking for interactive session: $cmd\n";
	my $installer_process = `$cmd`;
	chomp($installer_process);
	print "POSTFLIGHT::main> Installer Process: $installer_process\n";
	my $running_interactive = 0;
	if ($installer_process =~ /Installer.app/)
	{
		$running_interactive = 1;
	}

	if (scalar(@PSpluginDirs) == 0 && scalar(@elementsPluginDirs) == 0 && $running_interactive == 1)
	{
		print "POSTFLIGHT::main> No Photoshop installations found, prompt user\n";
		# put up an alert here to tell them that we are not going to install into a plug-in folder but that the 
		# plug-ins can be found in the product folder in Applications if they want to do a manual install
		my $alert_message = "An Installation of Photoshop or Elements could not be found. $FULL_PRODUCT_NAME will not be installed into a Photoshop or Elements plug-in folder at this time.\n\nYou can drag the files in '/Applications/$FULL_PRODUCT_NAME/Plug-in Files' to an appropriate plug-in folder later if you wish.";
		`"$INST_TOOL" "alert" "$alert_message"`;
	}


	if($SUITE_INSTALL == 1 || $GENUINEFRACTALS_INSTALL == 1)
	{
		# do Genuine Fractals

		if($STANDARD_INSTALL == 1)
		{
			$FULL_PRODUCT_NAME = "Perfect Resize 7";
			$FULL_PLUGIN_NAME = "Perfect Resize 7.plugin";
			$PLUG_IN_SOURCE = "/Applications/Perfect Resize 7/Plug-in Files";
			@PRODUCT_FOLDERS_TO_DELETE = (
				"Perfect Resize 7.plugin",
			);
			
			$HAS_SIXTY_FOUR_BIT_HARNESS = 0;
		
			# do the Photoshop installs
			@PLUGIN_DIRS = @PSpluginDirs;
			doCopy();
		
			# do the Elements installs
			@PLUGIN_DIRS = @elementsPluginDirs;
			doCopy();
		
			CleanPerms();
		}
		else
		{
			$FULL_PRODUCT_NAME = "Perfect Resize 7 Professional Edition";
			$FULL_PLUGIN_NAME = "Perfect Resize 7 Professional Edition.plugin";
			$PLUG_IN_SOURCE = "/Applications/Perfect Resize 7 Professional Edition/Plug-in Files";
			@PRODUCT_FOLDERS_TO_DELETE = (
				"Perfect Resize 7 Professional Edition.plugin",
				"Perfect Resize 7.plugin",
			);
			
			$HAS_SIXTY_FOUR_BIT_HARNESS = 0;
		
			# do the Photoshop installs
			@PLUGIN_DIRS = @PSpluginDirs;
			doCopy();
		
			# do the Elements installs
			@PLUGIN_DIRS = @elementsPluginDirs;
			doCopy();
		
			CleanPerms();
		}
	}

	if($SUITE_INSTALL == 1 || $PHOTOFRAME_INSTALL == 1)
	{
		# do PhotoFrame

		$FULL_PRODUCT_NAME = "PhotoFrame 4.6 Professional Edition";
		$FULL_PLUGIN_NAME = "PhotoFrame 4.6 Professional Edition.plugin";
		$PLUG_IN_SOURCE = "/Applications/PhotoFrame 4.6 Professional Edition/Plug-in Files";
		@PRODUCT_FOLDERS_TO_DELETE = (
			"PhotoFrame 4.6 Professional Edition.plugin",
			"PhotoFrame 4.6.plugin",
			"PhotoFrame 4.6 Free",
			"PhotoFrame 4.6 Free.plugin",
			"PhotoFrame 4.6 Free 64.plugin",
			"PhotoFrame 4.6 64.plugin",
			"PhotoFrame 4.6 Professional Edition 64.plugin",
		);
		
		$HAS_SIXTY_FOUR_BIT_HARNESS = 1;
		$FULL_PLUGIN_x32 = "/PhotoFrame 4.6 Professional Edition.plugin/PhotoFrame 4.6 Professional Edition.plugin/Contents/Plugins/PhotoFrame 4.6 Professional Edition.plugin";
		
		# do the Photoshop installs
		@PLUGIN_DIRS = @PSpluginDirs;
		doCopy();
	
		# do the Elements installs
		@PLUGIN_DIRS = @elementsPluginDirs;
		doCopy();
	
		if($UPDATE_INSTALL == 0)
		{
			#photoframe presets are too big to include in updater

			#install presets - not really sure why each preset subdir is handled individually
			$SHOULD_COPY_PREFS_INSTEAD_OF_MOVE = 1;
			$SHOULD_DELETE_PREFS_SOURCE = 0;
			$PREFS_SOURCE = "/Applications/$FULL_PRODUCT_NAME/Presets";
			$PREFS_DEST = "Application Support/onOne Software/PhotoFrame 4.6/Presets";
			$OLD_PREFS_FOLDER	= "Application Support/onOne Software/PhotoFrame 4.5/Presets";
			$PREFS_ROOT = "Application Support/onOne Software";
			@PREFERENCE_FILES = (
				"Helene Glassman",
				"Jim DiVitale",
				"Kevin Kubota",
				"Last Used",
				"Laurie Excell",
				"onOne Exchange",
				"Rick Sammon",
			);
			foreach $pref_file (@PREFERENCE_FILES)
			{
				copyPrefs($pref_file);
			}
		}
		
		CleanPerms();
	}

	if($SUITE_INSTALL == 1 || $FOCALPOINT_INSTALL == 1)
	{
		# do FocalPoint

		$FULL_PRODUCT_NAME = "FocalPoint 2";
		$FULL_PLUGIN_NAME = "FocalPoint 2.plugin";
		$PLUG_IN_SOURCE = "/Applications/FocalPoint 2/Plug-in Files";
		@PRODUCT_FOLDERS_TO_DELETE = (
			"FocalPoint 2.plugin",
		);
		
		$HAS_SIXTY_FOUR_BIT_HARNESS = 0;
		
		# do the Photoshop installs
		@PLUGIN_DIRS = @PSpluginDirs;
		doCopy();
	
		# do the Elements installs
		@PLUGIN_DIRS = @elementsPluginDirs;
		doCopy();
	
		#install presets
		$SHOULD_COPY_PREFS_INSTEAD_OF_MOVE = 1;
		$SHOULD_DELETE_PREFS_SOURCE = 0;
		$PREFS_SOURCE = "$PLUG_IN_SOURCE/Presets";
		$PREFS_DEST = "Application Support/onOne Software/FocalPoint 2/Presets";
		$OLD_PREFS_FOLDER	= "";
		$PREFS_ROOT = "Application Support/onOne Software";
		@PREFERENCE_FILES = (
			"onOne Presets",
			"NAPP",
		);
		foreach $pref_file (@PREFERENCE_FILES)
		{
			copyPrefs($pref_file);
		}
		
		CleanPerms();
	}

	if($SUITE_INSTALL == 1 || $PERFECTLAYERS_INSTALL == 1)
	{
		# Perfect Layers

		#not much to do here, but set it up anyway

		$FULL_PRODUCT_NAME = "Perfect Layers";

		$FULL_PLUGIN_NAME = "Perfect Effects Free.plugin";		#layers now installs effects free
		if (-d "/Applications/Perfect Effects" || -f "/Applications/Perfect Effects")
		{
			#don't install effeects free plugin since full effects is installed
			$FULL_PLUGIN_NAME = "";		
		}

		$PLUG_IN_SOURCE = "/Applications/Perfect Layers/Plug-in Files";
		@PRODUCT_FOLDERS_TO_DELETE = (
			"Perfect Layers",
			"Perfect Effects Free.plugin",
		);
		
		$HAS_SIXTY_FOUR_BIT_HARNESS = 0;

		if($SUITE_INSTALL == 0 && length($FULL_PLUGIN_NAME) > 0)
		{
			#layers standalone now installs effects free plugin
			
			# do the Photoshop installs
			@PLUGIN_DIRS = @PSpluginDirs;
			doCopy();
		
			# do the Elements installs
			@PLUGIN_DIRS = @elementsPluginDirs;
			doCopy();
		}
		
		CleanPerms();

		### turn this off for PerfectLayers 1.5 ###
		#get rid of preview prefs (if any)
		$SHOULD_DELETE_EXISTING_PREFS = 0;
		$EXISTING_PREFS_PATH = "Library/Preferences/com.ononesoftware.PerfectLayers.plist";
		DeletePrefs();

		if (-d "/Applications/Perfect Effects" || -f "/Applications/Perfect Effects")
		{
			#need to remove Effects Free lrplugin installed for layers - don't need it since Effects Full is installed - include both names just in case
			if (-d "/Library/Application Support/Adobe/Lightroom/Modules/Perfect Effects Free.lrplugin" || -f "/Library/Application Support/Adobe/Lightroom/Modules/Perfect Effects Free.lrplugin")
			{
				$cmd = "rm -rf \"/Library/Application Support/Adobe/Lightroom/Modules/Perfect Effects Free.lrplugin\"";					#effectsfree installer
				print `$cmd`;
			}
			if (-d "/Library/Application Support/Adobe/Lightroom/Modules/PerfectEffects Free For Layers.lrplugin" || -f "/Library/Application Support/Adobe/Lightroom/Modules/PerfectEffects Free For Layers.lrplugin")
			{
				$cmd = "rm -rf \"/Library/Application Support/Adobe/Lightroom/Modules/PerfectEffects Free For Layers.lrplugin\"";		#layers installer
				print `$cmd`;
			}
		}
	}

	if($SUITE_INSTALL == 1 || $PERFECTEFFECTS_INSTALL == 1)
	{
		if($FREE_INSTALL == 1)
		{
			# do Perfect Effects Free
	
			$FULL_PRODUCT_NAME = "Perfect Effects Free";
			$FULL_PLUGIN_NAME = "Perfect Effects Free.plugin";
			$PLUG_IN_SOURCE = "/Applications/Perfect Effects Free/Plug-in Files";
			@PRODUCT_FOLDERS_TO_DELETE = (
				"Perfect Effects Free.plugin",
			);
			
			$HAS_SIXTY_FOUR_BIT_HARNESS = 0;
			
			# do the Photoshop installs
			@PLUGIN_DIRS = @PSpluginDirs;
			doCopy();
		
			# do the Elements installs
			@PLUGIN_DIRS = @elementsPluginDirs;
			doCopy();
		
			CleanPerms();
		}
		else
		{
			# do Perfect Effects
	
			$FULL_PRODUCT_NAME = "Perfect Effects";
			$FULL_PLUGIN_NAME = "Perfect Effects.plugin";
			$PLUG_IN_SOURCE = "/Applications/Perfect Effects/Plug-in Files";
			@PRODUCT_FOLDERS_TO_DELETE = (
				"Perfect Effects.plugin",
				"Perfect Effects Free.plugin",
			);
			
			$HAS_SIXTY_FOUR_BIT_HARNESS = 0;
			
			# do the Photoshop installs
			@PLUGIN_DIRS = @PSpluginDirs;
			doCopy();
		
			# do the Elements installs
			@PLUGIN_DIRS = @elementsPluginDirs;
			doCopy();
		
			CleanPerms();
		}
	}

	if($SUITE_INSTALL == 1 || $PERFECTMASK_INSTALL == 1)
	{
		# do Perfect Mask

		$FULL_PRODUCT_NAME = "Perfect Mask";
		$FULL_PLUGIN_NAME = "Perfect Mask.plugin";
		$PLUG_IN_SOURCE = "/Applications/Perfect Mask/Plug-in Files";
		@PRODUCT_FOLDERS_TO_DELETE = (
			"Perfect Mask.plugin",
		);
		
		$HAS_SIXTY_FOUR_BIT_HARNESS = 0;
		
		# do the Photoshop installs
		@PLUGIN_DIRS = @PSpluginDirs;
		doCopy();
	
		# do the Elements installs
		@PLUGIN_DIRS = @elementsPluginDirs;
		doCopy();
	
		CleanPerms();
	}

	if($SUITE_INSTALL == 1 || $PERFECTPORTRAIT_INSTALL == 1)
	{
		# do Perfect Portrait

		$FULL_PRODUCT_NAME = "Perfect Portrait";
		$FULL_PLUGIN_NAME = "Perfect Portrait.plugin";
		$PLUG_IN_SOURCE = "/Applications/Perfect Portrait/Plug-in Files";
		@PRODUCT_FOLDERS_TO_DELETE = (
			"Perfect Portrait.plugin",
		);
		
		$HAS_SIXTY_FOUR_BIT_HARNESS = 0;
		
		# do the Photoshop installs
		@PLUGIN_DIRS = @PSpluginDirs;
		doCopy();
	
		# do the Elements installs
		@PLUGIN_DIRS = @elementsPluginDirs;
		doCopy();
	
		CleanPerms();
	}

	$FULL_PRODUCT_NAME = $FULL_PRODUCT_NAME_ORIG;			#reset
	$FULL_PLUGIN_NAME = $FULL_PLUGIN_NAME_ORIG;
	$PLUG_IN_SOURCE = $PLUG_IN_SOURCE_ORIG;

	print "POSTFLIGHT::doPluginInstall> success.\n";
}


###############################################################################
# main
###############################################################################
sub main
{
	print "POSTFLIGHT::main>\n";

	doPluginInstall();

	if($SUITE_INSTALL == 1)
	{
		#create new suite aliases in /Applications
		if (-d "/Applications/Perfect Photo Suite 6")
		{
			if (-d "/Applications/Perfect Mask")
			{
				$cmd = "ln -s \"/Applications/Perfect Mask\" \"/Applications/Perfect Photo Suite 6/Perfect Mask\"";
				print "POSTFLIGHT::main> $cmd\n";
				print `$cmd`;
			}
		
			if (-d "/Applications/Perfect Effects")
			{
				$cmd = "ln -s \"/Applications/Perfect Effects\" \"/Applications/Perfect Photo Suite 6/Perfect Effects\"";
				print "POSTFLIGHT::main> $cmd\n";
				print `$cmd`;
			}
		
			if (-d "/Applications/Perfect Portrait")
			{
				$cmd = "ln -s \"/Applications/Perfect Portrait\" \"/Applications/Perfect Photo Suite 6/Perfect Portrait\"";
				print "POSTFLIGHT::main> $cmd\n";
				print `$cmd`;
			}
		
			if (-d "/Applications/Perfect Resize 7 Professional Edition")
			{
				$cmd = "ln -s \"/Applications/Perfect Resize 7 Professional Edition\" \"/Applications/Perfect Photo Suite 6/Perfect Resize 7 Professional Edition\"";
				print "POSTFLIGHT::main> $cmd\n";
				print `$cmd`;
			}
		
			if (-d "/Applications/PhotoFrame 4.6 Professional Edition")
			{
				$cmd = "ln -s \"/Applications/PhotoFrame 4.6 Professional Edition\" \"/Applications/Perfect Photo Suite 6/PhotoFrame 4.6 Professional Edition\"";
				print "POSTFLIGHT::main> $cmd\n";
				print `$cmd`;
			}
		
			if (-d "/Applications/FocalPoint 2")
			{
				$cmd = "ln -s \"/Applications/FocalPoint 2\" \"/Applications/Perfect Photo Suite 6/FocalPoint 2\"";
				print "POSTFLIGHT::main> $cmd\n";
				print `$cmd`;
			}
		}
	}

	#create alias to layers in appsupport for new plugins (layers, effects, mask, portrait)
	#only need one alias for all new plugins, not one per product
	if ($SHOULD_CREATE_ALIAS)
	{
		#create alias in Application Support folder so Bridge only sees one app

		my $pathAliasDest = "";
		if(length($APPSUPPORT_ALIAS_DEST) > 0)
		{
			$pathAliasDest = $APPSUPPORT_ALIAS_DEST;
		}
		else
		{
			$pathAliasDest = "/Library/Application Support/onOne Software/$FULL_PRODUCT_FOLDER_NAME";
		}

		my $pathAliasSrc  = "/Applications/$FULL_PRODUCT_FOLDER_NAME";
		if (-d "$pathAliasDest/$FULL_APP_NAME")
		{
			#delete any existing app before creating an alias
			#full app was copied to Application Support (5.5, 5.5.1)
			`rm -rf \"$pathAliasDest/$FULL_APP_NAME\"`
		}		

		if (-d "$pathAliasSrc")
		{
			$cmd = "ln -f -s \"$pathAliasSrc/$FULL_APP_NAME/Contents/MacOS/$FULL_EXE_NAME\" \"$pathAliasDest/Perfect Layers\"";
			print "POSTFLIGHT::main> $cmd\n";
			print `$cmd`;
		}
	}

	#create alias to old vcbstandalones in appsupport for old plugins (resize, focalpoint, photoframe)
	#need an alias for each old plugin
	if($SUITE_INSTALL == 1 || $FOCALPOINT_INSTALL == 1)
	{
		#create alias in Application Support folder so Bridge only sees one app
		my $pathAliasDest = "/Library/Application Support/onOne Software/FocalPoint 2";
		my $nameAlias = "FocalPoint 2.app";
		if (-d "$pathAliasDest")
		{
			if (-d "$pathAliasDest/$nameAlias")
			{
				#delete any existing app before creating an alias
				#full app was copied to Application Support (5.5, 5.5.1)
				`rm -rf \"$pathAliasDest/$nameAlias\"`
			}		
	
			#space in app bundle name introduced to fix timemachine bug in suite 5.5
			my $pathAliasSrc = "/Applications/FocalPoint 2/FocalPoint 2 .app";
			if (-d "$pathAliasSrc")
			{
				$cmd = "ln -f -s \"$pathAliasSrc\" \"$pathAliasDest/$nameAlias\"";
				print "POSTFLIGHT::main> $cmd\n";
				print `$cmd`;
			}
		}
	}

	#create alias to old vcbstandalones in appsupport for old plugins (resize, focalpoint, photoframe)
	#need an alias for each old plugin
	if($SUITE_INSTALL == 1 || $GENUINEFRACTALS_INSTALL == 1)
	{
		#create alias in Application Support folder so Bridge only sees one app
		my $pathAliasDest = "/Library/Application Support/onOne Software/Perfect Resize";
		my $nameAlias = "PerfectResize.app";
		if (-d "$pathAliasDest")
		{
			if (-d "$pathAliasDest/$nameAlias")
			{
				#delete any existing app before creating an alias
				#full app was copied to Application Support (5.5, 5.5.1)
				`rm -rf \"$pathAliasDest/$nameAlias\"`
			}		
	
			my $pathAliasSrc = "/Applications/Perfect Resize 7 Professional Edition/Perfect Resize 7 Professional Edition .app";
			if (-d "$pathAliasSrc")
			{
				#pro version
				$cmd = "ln -f -s \"$pathAliasSrc\" \"$pathAliasDest/$nameAlias\"";
				print "POSTFLIGHT::main> $cmd\n";
				print `$cmd`;
			}
			else
			{
				$pathAliasSrc = "/Applications/Perfect Resize 7/Perfect Resize 7 .app";
				if (-d "$pathAliasSrc")
				{
					#standard version
					$cmd = "ln -f -s \"$pathAliasSrc\" \"$pathAliasDest/$nameAlias\"";
					print "POSTFLIGHT::main> $cmd\n";
					print `$cmd`;
				}
			}
		}
	}

	#create alias to old vcbstandalones in appsupport for old plugins (resize, focalpoint, photoframe)
	#need an alias for each old plugin
	if($SUITE_INSTALL == 1 || $PHOTOFRAME_INSTALL == 1)
	{
		#create alias in Application Support folder so Bridge only sees one app
		my $pathAliasDest = "/Library/Application Support/onOne Software/PhotoFrame 4.6";
		my $nameAlias = "PhotoFrameStandAlone.app";
		if (-d "$pathAliasDest")
		{
			if (-d "$pathAliasDest/$nameAlias")
			{
				#delete any existing app before creating an alias
				#full app was copied to Application Support (5.5, 5.5.1)
				`rm -rf \"$pathAliasDest/$nameAlias\"`
			}		
	
			my $pathAliasSrc = "/Applications/PhotoFrame 4.6 Professional Edition/PhotoFrame 4.6 Professional Edition .app";
			if (-d "$pathAliasSrc")
			{
				$cmd = "ln -f -s \"$pathAliasSrc\" \"$pathAliasDest/$nameAlias\"";
				print "POSTFLIGHT::main> $cmd\n";
				print `$cmd`;
			}
		}
	}
	
	#######################################################################
	# See if the Installer is running for this user, if so go ahead and
	# do some interactive things on the system
	# This is to prevent us, for example, from opening a Finder window
	# with root level priveleges on a logged out system
	#######################################################################
	my $my_name = MyUsername();
	$cmd = "ps -U $my_name -w | awk '/Installer.app/ && !/awk/'";
	print "POSTFLIGHT::main> Checking for interactive session: $cmd\n";
	my $installer_process = `$cmd`;
	chomp($installer_process);
	print "POSTFLIGHT::main> Installer Process: $installer_process\n";
	if ($installer_process =~ /Installer.app/)
	{
		#######################################################################
		# Tell the Finder to update the product folder so it matches what is actually on disk
		#######################################################################
		print "POSTFLIGHT::Main> Updating Finder\n";
		$cmd = "osascript -e 'tell application \"Finder\"' -e 'update folder \"Applications:$FULL_PRODUCT_NAME\" of startup disk' -e 'end tell'";
		print "POSTFLIGHT::main> $cmd\n";
		print `$cmd`;
	
		#######################################################################
		# open the product Application window
		#######################################################################
		print "POSTFLIGHT::main> Opening window /Applications/$FULL_PRODUCT_NAME\n";
		if (-d "/Applications/$FULL_PRODUCT_NAME")
		{
			$cmd = "osascript -e 'tell application \"Finder\"' -e 'make new Finder window to folder \"Applications:$FULL_PRODUCT_NAME\" of startup disk' -e 'end tell'";
			print "POSTFLIGHT::main> $cmd\n";
			print `$cmd`;
			`osascript -e 'tell application "Installer"' -e "activate" -e 'end tell'`;
		}
	
		#######################################################################
		# Show the video intro
		#######################################################################
		if ( $SHOW_VIDEO_INTRO == 1 )
		{
			my $reg_result = `"$INST_TOOL" "video_intro" "$FULL_PRODUCT_NAME"`;
			chomp($reg_result);
	
			if ($reg_result =~ /yes/)
			{
				print "POSTFLIGHT::Main> Video intro\n";
				`open \"$VIDEO_INTRO_URL\"`;
			#	`osascript -e 'tell application "Installer"' -e "activate" -e 'end tell'`;
			}
		}
	
		#######################################################################
		# Launch the registration app
		#######################################################################
		if ($SHOULD_LAUNCH_REG_APP)
		{
			print "POSTFLIGHT::Main> Launch registration app\n";
			$cmd = "osascript -e 'tell application \"Finder\"' -e 'open application file \"Applications:$FULL_PRODUCT_NAME:$REG_APP_NAME\" of startup disk' -e 'end tell'";
			print "POSTFLIGHT::main> $cmd\n";
			print `$cmd`;
		}
	}


	print "POSTFLIGHT::main> success.\n";
	exit(0);
}













































